
Ext.onReady(function() {
	
	$('#apply_logout').click(function() {		//注销用户
		$('#apply_logout').text('正在注销...');
		
		Ext.Ajax.request({
			url: '/szpxt-beta/apply_logout',
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				if(obj.success) {
					document.location = '/szpxt-beta/main/home.jsp';
				}
			}
		});
	});
	
	//按钮
	$('.submit, .clearForm').append('<span class="hover"></span>').each(function() {
		var $span = $('> span.hover', this).css('opacity', 0);
		$(this).hover(function() {
			$span.stop().fadeTo(500, 1);
		}, function() {
			$span.stop().fadeTo(500, 0);
		})
	});
	
	$('.submit').click(function() {		//提交保存
		if(Ext.getCmp('extForm').getForm().isValid()) {
			Ext.getCmp('extForm').getForm().submit({
				url: '/szpxt-beta/apply_create',
				success: function(form, action) {
					var serialNumber = action.result.serialNumber;
					
					$('#messageFalse').hide();		//提示信息
					$('#messageRight').show();
					$('#messageRightText').text('保存成功，序列号是"' + serialNumber + '！"');
				},
				failure: function() {
					$('#messageRight').hide();		//提示信息
					$('#messageFalse').show();
					$('#messageFalseText').text('保存失败！');
				}
			});
		}
	})
	
	$('.clearForm').click(function() {
		Ext.getCmp('extForm').getForm().reset();	//清空
		Ext.getCmp('extForm').getForm().clearInvalid();
	})
	
	$('#messageRight, #messageFalse').each(function() {
		$(this).click(function() {
			$(this).hide();
		})
	})
	
	showForm();
})

//显示表格
function showForm() {
	var comboAnalyst = {id: 'comboAnalyst', xtype: 'combo', fieldLabel: '分析师', hiddenName: 'userId', displayField: 'name', valueField: 'id', 
		store: new Ext.data.JsonStore({
			autoLoad: true,
			url: '/szpxt-beta/apply_getAnalysts',
			root: 'data',
			fields: ['id', 'name']
	}), triggerAction: 'all'};
	
	new Ext.form.FormPanel({
		id: 'extForm',
		baseCls: 'x-plain',
		width: 400,
		labelAlign: 'right',
		defaults: {xtype: 'textfield', width: 200, allowBlank: false},
		items: [
			{fieldLabel: '申请人姓名', name: 'name'},
			{fieldLabel: '公司名称', name: 'company'},
			{xtype: 'radiogroup', fieldLabel: '选择分析师', items: [
				{boxLabel: '是', name: 'radioAnalysts', listeners: {check: function(radio, checked) {
					if(checked) {
						Ext.getCmp('extForm').insert(3, comboAnalyst);
						Ext.getCmp('extForm').doLayout();
					}
				}}},
				{boxLabel: '否', name: 'radioAnalysts', checked: true, listeners: {check: function(radio, checked) {
					if(checked) {
						Ext.getCmp('extForm').remove('comboAnalyst');
						Ext.getCmp('extForm').doLayout();
					}
				}}}
			]},			
			{xtype: 'numberfield', fieldLabel: '目标融资额', name: 'amount', allowNegative: false},
			{fieldLabel: '移动电话', name: 'mobile'},
			{fieldLabel: '电子邮件', name: 'eMail', vtype: 'email'}
		],
		renderTo: 'form'
	});
}
